package tk.djcrazy.instantmessage.serviceImpl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.djcrazy.instantmessage.dao.IDao;
import tk.djcrazy.instantmessage.domain.FriendShip;
import tk.djcrazy.instantmessage.domain.UserEntity;
import tk.djcrazy.instantmessage.service.IFriendGroupService;
import tk.djcrazy.instantmessage.service.IFriendshipService;
import tk.djcrazy.instantmessage.service.IUserService;
@Service
public class FriendshipServiceImpl extends ServiceImpl<FriendShip> implements
		IFriendshipService  {
	@Resource
	private IUserService  userService;
	@Resource
	private IFriendGroupService friendGroupService;
	
 	@Override
	public void create(FriendShip baseBean) {
		getDao().create(baseBean);
	}

	@Override
	public void delete(FriendShip baseBean) {
		getDao().delete(baseBean);
	}
	
	@Override
	public void deleteFriendShip(int master, int slave) {
		getDao().delete(findFriendShip(master, slave));
	}

	@Override
	public void setFriendNickName(int masterId, int slaveId, String nickName) {
		FriendShip ship = findFriendShip(masterId, slaveId);
		ship.setNickName(nickName);
		getDao().save(ship);
	}

	@Override
	public FriendShip findFriendShip(int masterId, int slaveId) {
		return getDao().queryUnique("from FriendShip f where f.masterUser.id=? and f.slaveUser.id=?", masterId, slaveId);
	}

	@Override
	public void addFriendShipById(int master, int slave) {
		FriendShip friendShip = new FriendShip();
		UserEntity masterEntity = userService.findById(master);
		UserEntity slaveEntity = userService.findById(slave);
		friendShip.setMasterUser(masterEntity);
		friendShip.setSlaveUser(slaveEntity);
		friendShip.setDateCreated(new Date());
		this.create(friendShip);
	}

	@Override
	public void addFriendShipById(int master, int slave, String nickName) {
		FriendShip friendShip = new FriendShip();
		UserEntity masterEntity = userService.findById(master);
		UserEntity slaveEntity = userService.findById(slave);
		friendShip.setMasterUser(masterEntity);
		friendShip.setSlaveUser(slaveEntity);
		friendShip.setNickName(nickName);
		friendShip.setDateCreated(new Date());
		this.create(friendShip);
	}

	@Override
	public void addFriendShipByName(int uid, String friendName, int groupId) {
		FriendShip friendShip = new FriendShip();
		UserEntity masterEntity = userService.findById(uid);
		UserEntity slaveEntity = userService.findByUserName(friendName);
		friendShip.setMasterUser(masterEntity);
		friendShip.setSlaveUser(slaveEntity);
		friendShip.setDateCreated(new Date());
		this.create(friendShip);
		friendGroupService.addFriendIntoGroup(groupId, slaveEntity.getId());
	}

	@Override
	public void addFriendShipByName(int uid, String friendName, String nickName, int groupId) {
		FriendShip friendShip = new FriendShip();
		UserEntity masterEntity = userService.findById(uid);
		UserEntity slaveEntity = userService.findByUserName(friendName);
		friendShip.setMasterUser(masterEntity);
		friendShip.setSlaveUser(slaveEntity);
		friendShip.setNickName(nickName);
		friendShip.setDateCreated(new Date());
		this.create(friendShip);
		friendGroupService.addFriendIntoGroup(groupId, slaveEntity.getId());
	}
	@Override
	public void setDao(IDao<FriendShip> dao) {
		super.setDao(dao);
	}
 
	public IFriendGroupService getFriendGroupService() {
		return friendGroupService;
	}

	public void setFriendGroupService(IFriendGroupService friendGroupService) {
		this.friendGroupService = friendGroupService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}


 }
